import { Injectable } from '@angular/core';


@Injectable()
export class LiftTypeService {
    constructor() { }

    // 通过设备标识获取状态
    arrtoObj(data) {
        if (data.length) {
            const res = {
                state: null,
                fault: null,
                netWork: null,
                runstate: null,
                maintain: null,
                alarmLevel: null,
                alarmTxt: ''
            };
            for (let i = 0; i < data.length; i++) {
                const Code = data[i].parameterCode ? parseInt(data[i].parameterCode, 10) : parseInt(data[i].deviceParameterId, 10);

                // 通讯状态
                if (Code === 9) {
                    if (parseInt(data[i].value, 10)) {
                        res.netWork = parseInt(data[i].value, 10);
                    } else if (parseInt(data[i].deviceValue, 10)) {
                        res.netWork = parseInt(data[i].deviceValue, 10);
                    } else if (parseInt(data[i].value, 10) === 0) {
                        res.netWork = 0;
                    } else if (parseInt(data[i].deviceValue, 10) === 0) {
                        res.netWork = 0;
                    }
                }
                // 运行状态
                if (Code === 1) {
                    if (parseInt(data[i].value, 10)) {
                        res.state = parseInt(data[i].value, 10);
                    } else if (parseInt(data[i].deviceValue, 10)) {
                        res.state = parseInt(data[i].deviceValue, 10);
                    } else if (parseInt(data[i].value, 10) === 0) {
                        res.state = 0;
                    } else if (parseInt(data[i].deviceValue, 10) === 0) {
                        res.state = 0;
                    }
                }
                // 故障状态
                if (1710 <= Code && 1719 >= Code) {
                    if (parseInt(data[i].value, 10)) {
                        res.fault = parseInt(data[i].value, 10);
                        res.alarmTxt += data[i].parameterName ? (data[i].parameterName + ',') : (data[i].deviceParameterName + ',');
                    } else if (parseInt(data[i].deviceValue, 10)) {
                        res.fault = parseInt(data[i].deviceValue, 10);
                        res.alarmTxt += data[i].parameterName ? (data[i].parameterName + ',') : (data[i].deviceParameterName + ',');
                    }
                } else if (Code === 2) {
                    if (parseInt(data[i].value, 10)) {
                        res.fault = parseInt(data[i].value, 10);
                        res.alarmTxt += data[i].parameterName ? (data[i].parameterName + ',') : (data[i].deviceParameterName + ',');
                    } else if (parseInt(data[i].deviceValue, 10)) {
                        res.fault = parseInt(data[i].deviceValue, 10);
                        res.alarmTxt += data[i].parameterName ? (data[i].parameterName + ',') : (data[i].deviceParameterName + ',');
                    }
                }


                // 上下行状态
                if (Code === 1704) {
                    if (parseInt(data[i].value, 10)) {
                        res.runstate = parseInt(data[i].value, 10);
                    } else if (parseInt(data[i].deviceValue, 10)) {
                        res.runstate = parseInt(data[i].deviceValue, 10);
                    } else if (parseInt(data[i].value, 10) === 0) {
                        res.runstate = 0;
                    } else if (parseInt(data[i].deviceValue, 10) === 0) {
                        res.runstate = 0;
                    }
                }
                // 维保状态
                if (Code === 1720) {
                    if (parseInt(data[i].value, 10)) {
                        res.maintain = parseInt(data[i].value, 10);
                    } else if (parseInt(data[i].deviceValue, 10)) {
                        res.maintain = parseInt(data[i].deviceValue, 10);
                    } else {
                        res.maintain = 0;
                    }
                }
            }
            res.alarmTxt = res.alarmTxt.substring(0, res.alarmTxt.length - 1);
            return res;
        } else {
            const res = {};
            return res;
        }
    }

    mappingState(res) {
        if (res.netWork === 0) {
            return { isRun: false, Roomimg: '', Deviceimg: '', color: '#FF1B1B', text: '通讯中断' };
        } else if (res.netWork === 1) {
            // 维修状态
            if (!res.maintain) {
                if (res.fault === 1) {
                    // 故障
                    return { isRun: true, Roomimg: '20035-2', Deviceimg: '10036', color: '#FF1B1B', text: res.alarmTxt };
                } else {
                    if (res.state === 0) {
                        // 停止
                        return { isRun: true, Roomimg: '20033', Deviceimg: '10039', color: '#555', text: '正常停梯', ontxt: '停止' };
                    } else if (res.state === 1) {
                        if (!res.runstate && res.runstate !== 0) {
                            return { isRun: true, Roomimg: '20032', Deviceimg: '10038', color: '#1FB5FF', ontxt: '运行' };
                        }
                        // 运行
                        // 停层和上下行
                        switch (res.runstate) {
                            case 0:
                                return { isRun: true, Roomimg: '20032', Deviceimg: '10038', color: '#1FB5FF', text: '停层' };
                            case 1:
                                return { isRun: true, Roomimg: '20031', Deviceimg: '10034', color: '#1FB5FF', text: '正常' };
                            case 2:
                                return { isRun: true, Roomimg: '20035-1', Deviceimg: '10035', color: '#1FB5FF', text: '正常' };
                        }
                    }
                }
            } else if (res.maintain === 1) {
                return { isRun: true, Roomimg: '20034', Deviceimg: '10037', color: '#FE9A00', text: '正在检修' };
            }
        }
    }

    observe(data) {
        return this.mappingState(this.arrtoObj(data));
    }
}
